package com.itheima.reggie.controller;

import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.entity.Page;
import com.itheima.reggie.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/**
 * @Author: dw
 * @Date 2022/3/9 14:47
 */
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired(required = false)
    private EmployeeService employeeService;

    @Autowired(required = false)
    private HttpSession session;

    /**
     * 用户登录
     * @param employee
     * @return
     */
    @PostMapping("/login")
    public R<Employee> login(@RequestBody Employee employee){
        Employee loginEmployee = employeeService.login(employee);

        return R.success(loginEmployee);
    }

    @PostMapping("/logout")
    public R<String> logout(){
        session.invalidate();
        return R.success("退出成功");
    }

    @GetMapping("/page")
    public R<Page<Employee>> findByPage(Integer page,Integer pageSize,String name){
        Page<Employee> employeePage = employeeService.findByPage(page, pageSize, name);
        return R.success(employeePage);
    }

    @PostMapping
    public R<String> save(@RequestBody Employee employee){
        employeeService.save(employee);
        return R.success("添加成功");
    }

    @RequestMapping("/{id}")
    public R<Employee> findById(@PathVariable Long id){
        Employee employee = employeeService.findById(id);
        return R.success(employee);
    }

    @PutMapping
    public R<String> update(@RequestBody Employee employee){
        employeeService.update(employee);
        return R.success("修改成功");
    }
    /*@PutMapping
    public R<String> update(@RequestBody Employee employee,HttpSession session){
        //1.获取当前操作人的id
        Long id = (Long) session.getAttribute("employee");

        //2.完善数据修改人
        employee.setUpdateUser(id);

        //3.调用业务修改
        employeeService.update(employee);

        //4.返回数据
        return R.success("修改成功");
    }*/


}
